SetGfxDriver 1:Graphics 1024,768,8,2:SeedRnd MilliSecs() :SetBuffer FrontBuffer() 
Global Arial,nArial,timer,nums,numz,numx,z,z1,errorflag,frames,iterated,lplen,lptr,lframes,literated,yadro,lyadro,prostir
yadro=256: prostir=4096
timer = CreateTimer(100) :nums = CreateBank(prostir+2):numz= CreateBank(yadro+2):numx= CreateBank(yadro+2):errors=False
For i = 0 To prostir-1 
t = Rand(0,yadro-1):PokeByte nums,i, t:PokeByte numz,t, PeekByte (numz,t) +1 
Next 
cnt=0:z=0:z1=1:stopflag=True:stopflag1=False:prestopflag1=False:counter=0:current=0:iterated=0:frames=0

findhuman():findtarget():lframes=0
While Not KeyHit(1) 
Cls  
set256257()
stat(iterated,frames)
 movehuman():findhuman():findtarget()
; checkerrors()
;If lframes<>frames 
label1()
;:lframes=frames
Flip 
;If KeyHit(57) > 0
 WaitMouse()
Wend  











Function checkerrors()
errors=0:Color 0,255,0
For i = 0 To yadro  
PokeByte numx,i, 0 
Next 
For i = 0 To prostir-1 
t = PeekByte (nums,i):PokeByte numx,t, PeekByte (numx,t) +1 
Next 
For i = 0 To yadro 
If  (PeekByte (numz,i) <> PeekByte (numx,i)) errors=errors+1:Color 255,0,0:flag=True
Next

Text 20,160,errors+" errors 255="+PeekByte (numz,127)+" 127="+PeekByte (numz,127)+" 64="+PeekByte (numz,64),False ,False 
If flag=True WaitMouse()
End Function

Function unentropy() 
ptr=0:plen=1:prev=0:cur=0:g=127
t=PeekByte (nums,0)
  If ((t And g) =g) Then  prev= 1 Else prev=0
For i=1 To prostir-1 Step 1
t=PeekByte (nums,i)
  If ((t And g) =g) Then cur = 1 Else cur=0 
  If ((cur=prev) And ((t And g)=g)) plen=plen+1 
  If ((cur<>prev) And (plen>lplen)) Then lplen=plen:plen=1:lptr=ptr:ptr=i:prev=cur:lyadro=yadro:literated=iterated:flag=True
  If (prev<>cur) prev=cur:ptr=i:plen=1
Next


Color 0,255,0:Text 20,170,lptr+" ptr, "+lplen+" plen, "+lyadro+" yadro, "+literated+" iterations, ",False ,False 
;If flag=True WaitMouse()
End Function


Function movehuman()
current=PeekByte (nums,z)
If z1<=z Then
   For i=z To prostir-2
      PokeByte nums,i,PeekByte (nums,i+1)
   Next
   PokeByte nums,prostir-1,PeekByte (nums,0)
   For i=0 To z1-1
      PokeByte nums,i,PeekByte (nums,i+1)
   Next
   frames=frames+1:iterated=iterated+1
Else
;   For i=z To z1-1
;      PokeByte nums,i,PeekByte (nums,i+1)
;   Next
;   iterated=iterated+1
   For i=z To z1-1
    If z1<prostir-2 And z>2 Then
          PokeByte nums,i,(PeekByte (nums,i+1) Xor 255) 
        Else
          PokeByte nums,i,PeekByte (nums,i+1)
       EndIf
   Next
   iterated=iterated+1
EndIf
;PokeByte nums,z1,current
If z1<prostir-2 And z>2 Then PokeByte nums,z1,(current Xor 255) Else PokeByte nums,z1,current
z=z1+1
If z>=prostir Then z=0
End Function


Function findhuman()
For i=z To prostir-1
  If PeekByte (nums,i)>((yadro Shr 1)-1) Then z=i:i=prostir:finded=True Else finded=False 
Next
If finded=False Then
For i=0 To z
 If PeekByte (nums,i)>((yadro Shr 1)-1) Then z=i:i=prostir:finded=True:frames=frames+1:iterated=iterated+1 Else finded=False 
Next
EndIf
End Function

Function findtarget()
PokeByte nums,prostir,PeekByte (nums,0):PokeByte nums,prostir+1,PeekByte (nums,1)
current = PeekByte (nums,z)
If PeekByte (nums,z)<PeekByte (nums,z+1) za1 = 0 Else za1=1
If PeekByte (nums,z)<PeekByte (nums,z+2) za2 = 0 Else za2=1
If PeekByte (nums,z+1)<PeekByte (nums,z+2) za3 = 0 Else za3=1
If PeekByte (nums,z)=PeekByte (nums,z+1) za1 = 2
If PeekByte (nums,z)=PeekByte (nums,z+2) za2 = 2
If PeekByte (nums,z+1)=PeekByte (nums,z+2) za3 = 2
For i =z+1 To prostir-1
If PeekByte (nums,i)<PeekByte (nums,i+1) a1 = 0 Else a1=1
If PeekByte (nums,i)<PeekByte (nums,i+2) a2 = 0 Else a2=1
If PeekByte (nums,i+1)<PeekByte (nums,i+2) a3 = 0 Else a3=1
If PeekByte (nums,i)=PeekByte (nums,i+1) a1 = 2
If PeekByte (nums,i)=PeekByte (nums,i+2) a2 = 2
If PeekByte (nums,i+1)=PeekByte (nums,i+2) a3 = 2
If(a3=za3) Then z1=i:i=prostir:finded=True Else finded=False 
Next
If finded=False Then
For i =0 To z
If PeekByte (nums,i)<PeekByte (nums,i+1) a1 = 0 Else a1=1
If PeekByte (nums,i)<PeekByte (nums,i+2) a2 = 0 Else a2=1
If PeekByte (nums,i+1)<PeekByte (nums,i+2) a3 = 0 Else a3=1
If PeekByte (nums,i)=PeekByte (nums,i+1) a1 = 2
If PeekByte (nums,i)=PeekByte (nums,i+2) a2 = 2
If PeekByte (nums,i+1)=PeekByte (nums,i+2) a3 = 2
If( a3=za3) Then z1=i:i=prostir:finded=True Else finded=False 
Next
EndIf
End Function

Function stat(iterated,frames)
Text 20,180,iterated+" iterations, "+z+" pointer",False ,False 
End Function

Function set256257()
PokeByte nums,prostir, PeekByte (nums,0):PokeByte nums,prostir+1, PeekByte (nums,1)
End Function



Function label1()
;Color 255,255,0
;For i = 0 To yadro-1
;   Text 9,18+i*7,PeekByte (numz,i),False ,False
;   PokeByte numx,i,0
;Next
cnt=0
For i1 = 0 To 63
 For i = 0 To 63
    If (PeekByte (nums,i+i1*64)>((yadro Shr 1)-1) )Then Color 0,255,0 Else Color 0,100,0
    PokeByte numx,PeekByte (nums,i+i1*16), PeekByte (numx,PeekByte (nums,i+i1*16)) +1 
    Text 9+18+i*18,18+i1*7,PeekByte (nums,i+i1*64),False ,False 
        If (i1*16+i=z) Color 255,0,0: Rect 9+18+i*10,18+i1*7,11,7,0
        If (i1*16+i=z1) Color 255,255,0: Rect 9+18+i*10,18+i1*7,11,7,0
    If (PeekByte (nums,cnt)>((yadro Shr 1)-1) )Then Color 0,255,0 Else Color 0,PeekByte (nums,cnt),0
    Plot i1+2,(yadro-1-i)+2
       cnt=cnt+1


Next
Next
unentropy()
;Color 0,255,0
;For i = 0 To yadro-1
;If (PeekByte (numx,i)<>PeekByte (numz,i)) Color 255,0,0:WaitKey(): Else Color 0,255,0 
;Text 0,18+i*7,PeekByte (numx,i),False ,False
;Next


End Function